<?php

use Drupal\ssy_index_products\Entity\SsyIndexProducts;

function ssy_index_products_ssy_index_products_entity_form($form, &$form_state, SsyIndexProducts $e, $op) {
  $form_state['build_info']['files']['form'] = drupal_get_path('module', 'ssy_index_products') . '/ssy_index_products.forms.inc';
  $form_state[SsyIndexProducts::class] = $e;

  $form['name'] = [
    '#type' => 'textfield',
    '#title' => t('名称'),
    '#default_value' => $e->name,
  ];
  $form['category_id'] = [
    '#type' => 'select',
    '#title' => t('对应分类'),
    '#options' => shusy_category_options(),
    '#default_value' => $e->category_id,
    '#chosen' => TRUE,
    '#required' => TRUE,
  ];
  $form['sort'] = [
    '#type' => 'textfield',
    '#title' => t('排序'),
    '#default_value' => $e->sort,
    '#element_validate' => ['element_validate_integer'],
  ];
  $form['status'] = [
    '#type' => 'checkbox',
    '#title' => t('启用'),
    '#default_value' => $e->status,
  ];

  field_attach_form($e->entityType(), $e, $form, $form_state);

  $form['actions'] = ['#type' => 'actions'];
  $form['actions']['submit'] = [
    '#type' => 'submit',
    '#value' => t('保存'),
  ];
  return $form;
}

function ssy_index_products_ssy_index_products_entity_form_submit($form, &$form_state) {
  $e = $form_state[SsyIndexProducts::class];
  if ($e instanceof SsyIndexProducts) {
    entity_form_submit_build_entity($e->entityType(), $e, $form, $form_state);
    $e->save();
  }
}

function ssy_index_products_ssy_index_products_delete_form($form, &$form_state, SsyIndexProducts $e) {
  $form_state['build_info']['files']['form'] = drupal_get_path('module', 'ssy_index_products') . '/ssy_index_products.forms.inc';
  $form_state[SsyIndexProducts::class] = $e;
  return confirm_form($form, t('Are you sure you want to delete %title?', ['%title' => $e->label()]), 'admin/content/ssy_index_products/' . $e->id, t('This action cannot be undone.'), t('Delete'), t('Cancel'));
}

function ssy_index_products_ssy_index_products_delete_form_submit($form, &$form_state) {
  $e = $form_state[SsyIndexProducts::class];
  if ($e instanceof SsyIndexProducts) {
    $e->delete();
  }
  $form_state['redirect'] = 'admin/content/ssy_index_products';
}

